﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using YJKRevitBase.WinForm;

namespace YJKArchUtils.BreakWallByLevel
{
	public class MyProgressBar
	{
		public bool IsStoped
		{
			get
			{
				return this.m_StopTag;
			}
		}

		public MyProgressBar(WindowHandle hWndRevit, List<KeyValuePair<int, string>> subTypeInfoList, string Caption, bool IsCancel)
		{
			this.m_ProgressForm = new System.Windows.Forms.Form();
			this.m_ProgressForm.KeyPreview = true;
			this.m_ProgressForm.MinimizeBox = false;
			this.m_ProgressForm.MaximizeBox = false;
			this.m_ProgressForm.FormBorderStyle = FormBorderStyle.FixedDialog;
			this.m_ProgressForm.StartPosition = FormStartPosition.CenterScreen;
			this.m_ProgressForm.ShowInTaskbar = false;
			this.m_ProgressForm.Width = 340;
			this.m_ProgressForm.Height = 140;
			this.m_ProgressForm.Text = Caption;
			this.m_ProgressForm.FormClosing += this.form_FormClosing;
			this.m_ProgressForm.KeyDown += this.form_KeyDown;
			this.m_MainLabel = new Label();
			this.m_MainLabel.Width = 300;
			this.m_MainLabel.Height = 18;
			this.m_MainLabel.Left = 16;
			this.m_MainLabel.Top = 15;
			this.m_MainLabel.Parent = this.m_ProgressForm;
			foreach (KeyValuePair<int, string> keyValuePair in subTypeInfoList)
			{
				this.m_CumMaximum += keyValuePair.Key;
			}
			this.m_MainProgressBar = new ProgressBar();
			this.m_MainProgressBar.Maximum = this.m_CumMaximum;
			this.m_SubTypeInfoList = subTypeInfoList;
			this.m_MainProgressBar.Left = 16;
			this.m_MainProgressBar.Top = 35;
			this.m_MainProgressBar.Width = 300;
			this.m_MainProgressBar.Parent = this.m_ProgressForm;
			this.m_MainProgressBar.Value = 0;
			if (IsCancel)
			{
				new Button
				{
					Text = "取消",
					DialogResult = DialogResult.Cancel,
					Left = 242,
					Top = 77,
					Parent = this.m_ProgressForm
				}.Click += this.btn_Click;
			}
			this.m_ProgressForm.Show(hWndRevit);
		}

		public bool ProgressStep(int step)
		{
			if (this.m_StopTag)
			{
				this.Dispose();
				return false;
			}
			if (this.m_Counter > this.m_MainProgressBar.Maximum)
			{
				this.Dispose();
				return false;
			}
			this.m_MainProgressBar.Value += step;
			this.m_MainLabel.Text = "目前总体完成:" + this.m_MainProgressBar.Value * 100 / this.m_MainProgressBar.Maximum + "%";
			Application.DoEvents();
			this.m_Counter++;
			return true;
		}

		private int GetCurrentSubTypeIndex()
		{
			int num = 0;
			int num2 = this.m_Counter;
			foreach (KeyValuePair<int, string> keyValuePair in this.m_SubTypeInfoList)
			{
				if (num2 < keyValuePair.Key)
				{
					return num;
				}
				num2 -= keyValuePair.Key;
				num++;
			}
			return num;
		}

		private void btn_Click(object sender, EventArgs e)
		{
			this.m_ProgressForm.Close();
		}

		private void form_KeyDown(object sender, KeyEventArgs e)
		{
			this.m_ProgressForm.Close();
		}

		private void form_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.Yes)
			{
				this.m_StopTag = false;
				e.Cancel = true;
				return;
			}
			this.m_StopTag = true;
		}

		public void Dispose()
		{
			if (this.m_ProgressForm != null)
			{
				this.m_MainProgressBar.Dispose();
				this.m_ProgressForm.Dispose();
			}
		}

		private List<KeyValuePair<int, string>> m_SubTypeInfoList = new List<KeyValuePair<int, string>>();

		private int m_CumMaximum;

		private int m_Counter;

		private System.Windows.Forms.Form m_ProgressForm;

		private ProgressBar m_MainProgressBar;

		private bool m_StopTag;

		private Label m_MainLabel;
	}
}
